package app.common.utils.cjt.ao;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 找到凭证列表请求参数
 * @author feizns
 * @since 2023/05/12
 */
@Data
public class FindVoucherListAo {

    /**
     * 分页参数大小
     */
    private Integer pageSize;

    /**
     * 页码，从0开始
     */
    private Integer pageIndex;

    /**
     * 选择字段
     */
    private List<String> selectFields = new ArrayList<>();

    /**
     * param dic
     */
    private Map<String, Object> paramDic = new LinkedHashMap<>();

    /**
     * @param index 指数
     * @param size 大小
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return {@link FindVoucherListAo}
     */
    public static FindVoucherListAo of(int index, int size, LocalDate startDate, LocalDate endDate) {
        FindVoucherListAo ao = new FindVoucherListAo();
        ao.setPageIndex(index);
        ao.setPageSize(size);
        ao.selectFields.add("code");
        ao.paramDic.put("SaleDelivery.VoucherDate", Arrays.asList(
                SaleDeliveryVoucherDate.from(startDate),
                SaleDeliveryVoucherDate.from(endDate)
        ));
        return ao;
    }

    /**
     * @param index 指数
     * @param size 大小
     * @return {@link FindVoucherListAo}
     */
    public static FindVoucherListAo of(int index, int size, LocalDate... dates) {
        FindVoucherListAo ao = new FindVoucherListAo();
        ao.setPageIndex(index);
        ao.setPageSize(size);
        ao.selectFields.add("code");
        ao.paramDic.put("SaleDelivery.VoucherDate", Arrays.stream(dates).map(SaleDeliveryVoucherDate::from).collect(Collectors.toList()));
        return ao;
    }

    /**
     * 销售凭证交付日期
     * @author feizns
     * @since 2023/05/12
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class SaleDeliveryVoucherDate {

        /**
         * 文本
         */
        private String text;

        /**
         * 价值
         */
        private String value;

        /**
         * 从
         * @return {@link SaleDeliveryVoucherDate}
         */
        public static SaleDeliveryVoucherDate from(LocalDate date) {
            SaleDeliveryVoucherDate vo = new SaleDeliveryVoucherDate();
            vo.setText(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
            vo.setValue(vo.text);
            return vo;
        }

    }

    /**
     * 违约
     * @return {@link FindVoucherListAo}
     */
    public static FindVoucherListAo ofDefault(LocalDate start, LocalDate end) {
        return FindVoucherListAo.of(0, 100, start, end);
    }

}
